/*
 * awire.h
 *
 *  Created on: Aug 2, 2013
 *      Author: Ken Arok
 *
 *      Revision history:
 *      ---------------------------------------------------------
 *      Date			|	Revised by		|	Description
 *      ---------------------------------------------------------
 *      1. Aug 2, 2013	|	Yosef			| New establishment
 *
 *
 *
 * \brief Hardware Abstraction Layer of aWire UART Controller UC3C0512C
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include "config_board.h"

#if BOARD_1_0_USED

#ifndef AWIRE_H_
#define AWIRE_H_

#include <avr32/io.h>
#include "compiler.h"

#ifdef __cplusplus
extern "C" {
#endif


/*! \name Mode of operation.
 */
//! @{
#define AWIRE_MODE_DISABLED			(0)
#define AWIRE_MODE_RECEIVE			(1)
#define AWIRE_MODE_TRANSMIT			(2)
#define AWIRE_MODE_RECEIVE_RESYNC	(3)
//! @}

/*! \name Mask and Offset for register setting.
 */
//! @{
#define AWIRE_CR_MODE_MASK			(0x00000003)
#define AWIRE_CR_MODE_OFFSET		(0)
#define AWIRE_SR_BUSY_MASK			(0x00000001)
#define AWIRE_SR_BUSY_OFFSET		(0)
#define AWIRE_SR_CENABLED_MASK		(0x00000004)
#define AWIRE_SR_CENABLED_OFFSET	(2)
#define AWIRE_SR_READYINT_MASK		(0x00000100)
#define AWIRE_SR_READYINT_OFFSET	(8)
#define AWIRE_SR_DREADYINT_MASK		(0x00000200)
#define AWIRE_SR_DREADYINT_OFFSET	(9)
#define AWIRE_SR_OVERRUN_MASK		(0x00000400)
#define AWIRE_SR_OVERRUN_OFFSET		(10)
#define AWIRE_SR_TRMIS_MASK			(0x00002000)
#define AWIRE_SR_TRMIS_OFFSET		(13)
#define AWIRE_SCR_READYINT_MASK		(0x00000100)
#define AWIRE_SCR_READYINT_OFFSET	(8)
#define AWIRE_SCR_DREADYINT_MASK	(0x00000200)
#define AWIRE_SCR_DREADYINT_OFFSET	(9)
#define AWIRE_SCR_OVERRUN_MASK		(0x00000400)
#define AWIRE_SCR_OVERRUN_OFFSET	(10)
#define AWIRE_SCR_TRMIS_MASK		(0x00002000)
#define AWIRE_SCR_TRMIS_OFFSET		(13)
#define AWIRE_IER_READYINT_MASK		(0x00000100)
#define AWIRE_IER_READYINT_OFFSET	(8)
#define AWIRE_IER_DREADYINT_MASK	(0x00000200)
#define AWIRE_IER_DREADYINT_OFFSET	(9)
#define AWIRE_IER_OVERRUN_MASK		(0x00000400)
#define AWIRE_IER_OVERRUN_OFFSET	(10)
#define AWIRE_IER_TRMIS_MASK		(0x00002000)
#define AWIRE_IER_TRMIS_OFFSET		(13)
#define AWIRE_IDR_READYINT_MASK		(0x00000100)
#define AWIRE_IDR_READYINT_OFFSET	(8)
#define AWIRE_IDR_DREADYINT_MASK	(0x00000200)
#define AWIRE_IDR_DREADYINT_OFFSET	(9)
#define AWIRE_IDR_OVERRUN_MASK		(0x00000400)
#define AWIRE_IDR_OVERRUN_OFFSET	(10)
#define AWIRE_IDR_TRMIS_MASK		(0x00002000)
#define AWIRE_IDR_TRMIS_OFFSET		(13)
#define AWIRE_IMR_READYINT_MASK		(0x00000100)
#define AWIRE_IMR_READYINT_OFFSET	(8)
#define AWIRE_IMR_DREADYINT_MASK	(0x00000200)
#define AWIRE_IMR_DREADYINT_OFFSET	(9)
#define AWIRE_IMR_OVERRUN_MASK		(0x00000400)
#define AWIRE_IMR_OVERRUN_OFFSET	(10)
#define AWIRE_IMR_TRMIS_MASK		(0x00002000)
#define AWIRE_IMR_TRMIS_OFFSET		(13)
#define AWIRE_CRR_CLKEN_MASK		(0x00000001)
#define AWIRE_CRR_CLKEN_OFFSET		(0)
#define AWIRE_THR_TXCHR_MASK		(0x000000ff)
#define AWIRE_THR_TXCHR_OFFSET		(0)
//! @}

enum awire_status_code {
	AWIRE_STATUS_OK					= 0, //!< Success
	AWIRE_ERR_IO_ERROR				= -1, //!< I/O error
	AWIRE_ERR_FLUSHED				= -2, //!< Request flushed from queue
	AWIRE_ERR_TIMEOUT				= -3, //!< Operation timed out
	AWIRE_ERR_BAD_DATA				= -4, //!< Data integrity check failed
	AWIRE_ERR_PROTOCOL				= -5, //!< Protocol error
	AWIRE_ERR_UNSUPPORTED_DEV		= -6, //!< Unsupported device
	AWIRE_ERR_NO_MEMORY				= -7, //!< Insufficient memory
	AWIRE_ERR_INVALID_ARG			= -8, //!< Invalid argument
	AWIRE_ERR_BAD_ADDRESS			= -9, //!< Bad address
	AWIRE_ERR_BUSY					= -10, //!< Resource is busy
	AWIRE_ERR_BAD_FORMAT			= -11, //!< Data format not recognized
	AWIRE_OPERATION_IN_PROGRESS		= -128,
};

//! Structure of aWire UART options.
typedef struct {
	// User interface mode
	unsigned long mode;
	// Baud rate of transmission
	unsigned long baudrate;

} awire_options_t;

//! AWire status code
typedef enum awire_status_code awire_status_code_t;

//! Type data of aWire UART interface.
typedef volatile avr32_aw_t *awire_if;



/** \brief Initialize aWire UART interface.
 *
 * \param _awire	Base address of the aWire instance.
 * \param _opt		Option setting for aWire UART.
 *
 * \return Status
 *	\retval	AWIRE_ERR_INVALID_ARG	Failed
 *	\retval	AWIRE_STATUS_OK			Success
 *
 */
extern awire_status_code_t awire_init(volatile avr32_aw_t *_awire, const awire_options_t *opt);

/** \brief Set operation mode of aWire UART interface.
 *
 * \param _awire	Base address of the aWire instance.
 * \param _opt		Option setting for aWire UART.
 *
 * \return Status
 *	\retval	AWIRE_ERR_INVALID_ARG	Failed
 *	\retval	AWIRE_STATUS_OK			Success
 *
 */
extern awire_status_code_t awire_set_mode(volatile avr32_aw_t *_awire, const awire_options_t *opt);


#ifdef __cplusplus
}
#endif

#endif /* AWIRE_H_ */

#endif /* BOARD_1_0_USED */
